#pragma once

#include <iostream>
#include <string>
#include <unordered_map>
#include <fstream>
#include "Log.hpp"

const std::string gpath = "./";
const std::string gfilename = "Dict.txt";
using namespace LogModule;

class Dictionary
{
public:
    Dictionary(const std::string &path = gpath, const std::string filename = gfilename)
        : _path(path),
          _filename(filename)
    {
        LoadDictionary();
    }
    //happy: 快乐的
    void SplitDictionarWord(const std::string& line,std::string* word,std::string* content,const std::string & sep)
    {
        auto pos = line.find(sep);
        *word = line.substr(0,pos);
        *content = line.substr(pos+2);
    }
    bool LoadDictionary()
    {
        std::string filepath = _path + _filename;
        std::ifstream infile(filepath.c_str(), std::ios::in);
        if (!infile.is_open())
        {
            LOG(LogLevel::ERROR) << "file open error";
            return false;
        }
        std::string line;
        while(std::getline(infile,line))
        {
            std::string word;
            std::string content;
            std::string sep = ": ";
            SplitDictionarWord(line,&word,&content,sep);
            Dict[word] = content;
        }
        // Print();
        return true;
    }
    void Print()
    {
        for (auto& x : Dict)
        {
            std::cout << x.first << " : " << x.second << std::endl;
        }
    }
    std::string TransLate(const std::string& word)
    {
        if (Dict.find(word) == Dict.end()) return "NONE";
        return Dict[word];
    }

    ~Dictionary()
    {
    }

private:
    std::unordered_map<std::string, std::string> Dict;
    std::string _path;
    std::string _filename;
};